﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MyGrouping.UrlTree
{
    public class Node : UrlNode
    {   
        private List<UrlNode> children;
        
        public List<UrlNode> Children
        {
            get { return children; }
            set { children = value; }
        }

        public Node(string _name) : base(_name)
        {
            children = new List<UrlNode>();
        }

        public Node(string _name, List<UrlNode> _children) : base(_name)
        {            
            children = _children;            
        }

        /// <summary>
        /// Create a new node and then add this child.
        /// </summary>
        /// <param name="nodeName">Name of the node</param>
        /// <returns>Newly added node</returns>
        public Node AddNode(string nodeName)
        {
            for (int i = 0; i < children.Count; i++)
            {
                if ((children[i].IsLeaf() == false)&&(children[i].Name.CompareTo(nodeName) == 0))
                {
                    return (Node)children[i];
                }
            }

            children.Add(new Node(nodeName));

            return (Node)children[children.Count - 1];
        }

        /// <summary>
        /// Add child node to this current node
        /// </summary>
        /// <param name="nodeChild">The adding child node</param>
        /// <returns>The added node</returns>
        public Node AddNode(Node nodeChild)
        {
            for (int i = 0; i < children.Count; i++)
            {
                if ((children[i].IsLeaf() == false) && (children[i].Name.CompareTo(nodeChild.Name) == 0))
                {
                    return (Node)children[i];                    
                }
            }

            children.Add(nodeChild);
            return (Node)children[children.Count - 1];
        }

        /// <summary>
        /// Add a leaf child.
        /// </summary>
        /// <param param name="leafChild">The adding leaf child</param>
        /// <returns>The added leaf child</returns>
        public Leaf AddLeaf(Leaf leafChild)
        {
            for (int i = 0; i < children.Count; i++)
            {
                if ((children[i].IsLeaf() == true) && (children[i].Name.CompareTo(leafChild.Name) == 0))
                {
                    return (Leaf)children[i];
                }
            }

            children.Add(leafChild);
            return (Leaf)children[children.Count - 1];
        }

        public override bool IsLeaf()
        {
            return false;
        }

        public override bool IsSingleDad()
        {
            if ((children.Count == 1) && (children[0].IsLeaf() == true))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override bool IsSingleAncestor()
        {
            if ((children.Count == 1) && (children[0].IsLeaf() == false))
            {
                return true;
            }
            else
            {
                return false;
            }
        }        
    }
}
